function [start, end] = GetTileRange(tile_num, index)

x_tiles = 8; % 8 tiles
x_tile_width = 15; % 8*15 = 120 (the width of the pitch)

y_tiles = 4;
y_tile_width = 15;

distance_tiles = 14; % distance varies between 0 and 135; 14*10 = 140
distance_tile_width = 10;

angle_tiles = 8; % 8*45 = 360
angle_tile_width = 45;
num_tilings = 32;

x_offset = num_tilings*(x_tiles+1) - 1;
y_offset = num_tilings*(y_tiles+1) - 1;
distance_offset = num_tilings*(distance_tiles+1) - 1;
angle_offset = num_tilings*(angle_tiles+1) - 1;

if(index_in_state == 1) % ball_x
    offset = 0;
    
elseif (index_in_state == 2) % ball_y
    offset = x_offset;
    index = index - offset;
    
elseif (rem(index,2) == 0) % player angle
    offset = x_offset + y_offset + (((index-4)/2)+1)*distance_offset + ((index-4)/2)*angle_offset;
    index = index - offset;
    
else % player distance    
    offset = x_offset + y_offset + ((index-3)/2)*distance_offset + ((index-3)/2)*angle_offset;
    index = index - offset;
    
end

tiling_num = GetTilingNum(index);
old_tilings = 1*x_tiles + (tiling_num-2)*(x_tiles+1); % number of tiles in the previous tilings
offset = ((tiling_num-1)/num_tilings)*x_tile_width; % offset from the first tile

end

function num = GetTilingNum(tile_num, num_tiles)
    if (tile_num <= num_tiles)
        num = 1;
    else
        for num = 2:32,
           if (num_tiles + (num-1)*(num_tiles+1) >= tile_num)
               break;
           end
        end
    end
end

function range = GetRange(new_pos, 

if(new_pos == old_tilings + 1)
    start_new = 0;
    finish_new = offset;
elseif(new_pos == old_tilings + 2)
    start_new = offset;
    finish_new = start_new + tile_width;
else
    start_new = offset + (new_pos - old_tilings - 2)*4;
    finish_new = start_new + tile_width;
end

if (start_new > start)
    start = start_new;
elseif (finish_new < finish)
    finish = finish_new;
end

end
